// miniprogram/pages/applications/addtype/addtype.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sort_list: [{
        name: '节日鲜花',
        id: 0,
        types: 1
      },
      {
        name: '鲜花花材',
        id: 1,
        types: 1
      },
      {
        name: '爱情鲜花',
        id: 2,
        types: 1
      },
      {
        name: '送花对象',
        id: 3,
        types: 1
      },
      {
        name: '鲜花类型',
        id: 4,
        types: 1
      },
      {
        name: '鲜花颜色',
        id: 5,
        types: 2
      },
      {
        name: '鲜花枝数',
        id: 6,
        types: 3
      },
      {
        name: '鲜花价格',
        id: 7,
        types: 4
      },
      {
        name: '花篮价格',
        id: 8,
        types: 4
      },
      {
        name: '开业/商务鲜花',
        id: 9,
        types: 1
      },
      {
        name: '企业鲜花',
        id: 10,
        types: 1
      },
      {
        name: '婚庆/婚礼',
        id: 11,
        types: 1
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },
  getSortList(e) {
    let idx = e.currentTarget.dataset.idx;
    let sort = this.data.sort_list;
    this.setData({
      sid: idx,
      sort: sort[idx]
    })
  },
  chooseImgs: function() { //这里是选取图片的方法
    var _this = this;
    wx.chooseImage({
      count: 1, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'], // original 原图，compressed 压缩图，默认二者都有
      sourceType: ['album', 'camera'], // album 从相册选图，camera 使用相机，默认二者都有
      success: function(res) {
        console.log(res);
        var imgpath = res.tempFiles[0].path;
        _this.setData({
          imgpath: imgpath
        })
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })

  },
  deleteImg() {
    this.setData({
      imgpath: '',
    })
  },
  uploadImg() {
    wx.showLoading({
      title: '图片上传中',
    })
    var _this = this;
    var date = new Date();
    console.log(date);
    var filePath = this.data.imgpath;
    var yy = date.getFullYear();
    var mm = date.getMonth() + 1;
    var dd = date.getDate();
    const cloudPath = yy + '' + mm + '' + dd + '' + parseInt(Math.random() * 10000000) + filePath.match(/\.[^.]+?$/)[0];
    console.log('cloudPath', cloudPath);
    wx.cloud.uploadFile({
      cloudPath,
      filePath,
      success: res => {
        console.log(res);
        _this.setData({
          imgurl: res.fileID
        })
        //console.log('[上传文件] 成功：', res);
        //_this.uploadMoreimgs();
        _this.uploadItemInfo();
      },
      fail: e => {
        fail++;
        console.error('[上传文件] 失败：', e)
      },
      complete: () => {

      }
    })
  },
  formSubmit(e) {
    console.log(e);
    let val = e.detail.value;
    for (let i in val) {
      if (!val[i]) {
        wx.showToast({
          title: '请检查表单',
        })
        return false;
      }
    }
    console.log('表单验证成功')
    this.setData({
      val: val
    })
    this.uploadImg();
    1
  },
  resetFormData(){
     console.log('表单重置了');
  },
  uploadItemInfo() {
    let val = this.data.val;
    let sort = this.data.sort;
    let db = wx.cloud.database();
    let imgurl = this.data.imgurl;
    val.type_id = sort.types;
    val.imgurl = imgurl;
    console.log(val);
    db.collection('tb_type_list').where({
      type_name: val.type_name
    }).get({
      success: res => {
        if (res.data.length > 0) {
          wx.showToast({
            title: '请勿重复添加',
          })
        } else {
          db.collection('tb_type_list').add({
            data: val,
            success: res => {
              console.log(res);
              wx.showToast({
                title: '添加成功',
              })
            },
            fail: err => {
              console.log(err);
            }
          })
        }
      },
      fail: err => {
        wx.showToast({
          title: '网络错误',
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})